# -*- coding:utf-8 -*-
#import sys
#reload(sys)
#sys.setdefaultencoding('utf-8')

import os,datetime
now_path=os.path.abspath('.').replace('\\','/')

log_file=now_path+'/xuliehua.json'
class C(object):
    c_cc='cc'
    c_list_c=['c','c']*5
class B(object):
    b_bb=9
    b_list_b=['b','b']
    #b_c=C()
    #b_list_c_object=[C()]*50
    #b_bzn='中文'
class A(object):
    '''int、string、Boolean、long、double,arraylist、object、DateTime、'''
    a_i=1
    a_s='a'
    #a_szn='中文'
    a_b=True
    a_li=[1,2,3]
    a_o=B()
    a_dt=datetime.datetime.now()
    a_l=9999L
    a_f=1.1

    def say_hi(self):
        print 'hi'
    def set_ii(self):
        self.ii=123
coding_list=['utf-8', 'gb2312', 'gbk', 'gb18030', 'cp1252', ]
def decode_str(data):
    for coding in coding_list:
        try:
            rdata=data.decode(coding)
            return rdata
        except:
            pass
    return u''
def encode_attr(tobject,svalue):
    if str(svalue).startswith('__'):
        return ''
    if hasattr(tobject,str(svalue)):
        value=getattr(tobject,str(svalue))
    else:
        value=svalue
    if '__call__' in dir(value):
        return ''
    if type(value) in (type(1),type(1L),type(1.0)):
        return '%s'%value
    elif type(value)==type(True):
        return '%s'%(str(value).lower())
    elif type(value)==type('s'):                    #str
        #print "%s:%s,"%(str(svalue),str(value))
        value=decode_str(value)
        print '"%s"'%value
        return '"%s"'%value
    elif type(value)==type([1]):                    #list
        return_list=[]
        list_str=''
        for i in value:
            #return_list.append(decode_attr(value,i))
            list_str=list_str+encode_attr(value,i)+','
        #print return_list
        return '[%s]'%list_str[:-1]
    elif type(value)==type(datetime.datetime.now()):
        return '"%s"'%str(value)
    elif '__class__' in dir(value):
        return encode_object(value)
    return ''


def encode_object(tobject):
    value_list=dir(tobject)
    #print value_list
    json_str=''
    for svalue in value_list:
        js_str=encode_attr(tobject,svalue)
        if js_str:
            json_str+='"%s":%s,'%(svalue,js_str)
    json_str=json_str[:-1]
    return "{%s}"%json_str

a=A()
a.set_ii()
json_str=encode_object(a)
print '='*50
print json_str.replace("'",'"')
f=open(log_file,'w')
f.write(json_str)
f.close()










